home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mg2a_src.zip / SEARCH.C < prev    next >
C/C++ Source or Header  |  1991-03-10  |  15KB  |  669 lines

  1. /*
  2.  *        Search commands.
  3.  * The functions in this file implement the
  4.  * search commands (both plain and incremental searches
  5.  * are supported) and the query-replace command.
  6.  *
  7.  * The plain old search code is part of the original
  8.  * MicroEMACS "distribution". The incremental search code,
  9.  * and the query-replace code, is by Rich Ellison.
  10.  */
  11. #include    "def.h"
  12. #ifndef NO_MACRO
  13. #include    "macro.h"
  14. #endif
  15.  
  16. #define SRCH_BEGIN    (0)            /* Search sub-codes.    */
  17. #define SRCH_FORW    (-1)
  18. #define SRCH_BACK    (-2)
  19. #define SRCH_NOPR    (-3)
  20. #define SRCH_ACCM    (-4)
  21. #define SRCH_MARK    (-5)
  22.  
  23. typedef struct    {
  24.     int    s_code;
  25.     LINE    *s_dotp;
  26.     int    s_doto;
  27. }    SRCHCOM;
  28.  
  29. static    SRCHCOM cmds[NSRCH];
  30. static    int    cip;
  31.  
  32. int    srch_lastdir = SRCH_NOPR;        /* Last search flags.    */
  33.  
  34. static VOID    is_cpush();
  35. static VOID    is_lpush();
  36. static VOID    is_pop();
  37. static int    is_peek();
  38. static VOID    is_undo();
  39. static int    is_find();
  40. static VOID    is_prompt();
  41. static VOID    is_dspl();
  42. static int    eq();
  43.  
  44. /*
  45.  * Search forward.
  46.  * Get a search string from the user, and search for it,
  47.  * starting at ".". If found, "." gets moved to just after the
  48.  * matched characters, and display does all the hard stuff.
  49.  * If not found, it just prints a message.
  50.  */
  51. /*ARGSUSED*/
  52. forwsearch(f, n)
  53. {
  54.     register int    s;
  55.  
  56.     if ((s=readpattern("Search")) != TRUE)
  57.         return s;
  58.     if (forwsrch() == FALSE) {
  59.         ewprintf("Search failed: \"%s\"", pat);
  60.         return FALSE;
  61.     }
  62.     srch_lastdir = SRCH_FORW;
  63.     return TRUE;
  64. }
  65.  
  66. /*
  67.  * Reverse search.
  68.  * Get a search string from the     user, and search, starting at "."
  69.  * and proceeding toward the front of the buffer. If found "." is left
  70.  * pointing at the first character of the pattern [the last character that
  71.  * was matched].
  72.  */
  73. /*ARGSUSED*/
  74. backsearch(f, n)
  75. {
  76.     register int    s;
  77.  
  78.     if ((s=readpattern("Search backward")) != TRUE)
  79.         return (s);
  80.     if (backsrch() == FALSE) {
  81.         ewprintf("Search failed: \"%s\"", pat);
  82.         return FALSE;
  83.     }
  84.     srch_lastdir = SRCH_BACK;
  85.     return TRUE;
  86. }
  87.  
  88. /*
  89.  * Search again, using the same search string
  90.  * and direction as the last search command. The direction
  91.  * has been saved in "srch_lastdir", so you know which way
  92.  * to go.
  93.  */
  94. /*ARGSUSED*/
  95. searchagain(f, n)
  96. {
  97.     if (srch_lastdir == SRCH_FORW) {
  98.         if (forwsrch() == FALSE) {
  99.             ewprintf("Search failed: \"%s\"", pat);
  100.             return FALSE;
  101.         }
  102.         return TRUE;
  103.     }
  104.     if (srch_lastdir == SRCH_BACK) {
  105.         if (backsrch() == FALSE) {
  106.             ewprintf("Search failed: \"%s\"", pat);
  107.             return FALSE;
  108.         }
  109.         return TRUE;
  110.     }
  111.     ewprintf("No last search");
  112.     return FALSE;
  113. }
  114.  
  115. /*
  116.  * Use incremental searching, initially in the forward direction.
  117.  * isearch ignores any explicit arguments.
  118.  */
  119. /*ARGSUSED*/
  120. forwisearch(f, n)
  121. {
  122.     return isearch(SRCH_FORW);
  123. }
  124.  
  125. /*
  126.  * Use incremental searching, initially in the reverse direction.
  127.  * isearch ignores any explicit arguments.
  128.  */
  129. /*ARGSUSED*/
  130. backisearch(f, n)
  131. {
  132.     return isearch(SRCH_BACK);
  133. }
  134.  
  135. /*
  136.  * Incremental Search.
  137.  *    dir is used as the initial direction to search.
  138.  *    ^S    switch direction to forward
  139.  *    ^R    switch direction to reverse
  140.  *    ^Q    quote next character (allows searching for ^N etc.)
  141.  *    <ESC>    exit from Isearch
  142.  *    <DEL>    undoes last character typed. (tricky job to do this correctly).
  143.  *    other ^ exit search, don't set mark
  144.  *    else    accumulate into search string
  145.  */
  146. isearch(dir) {
  147.     register int    c;
  148.     register LINE    *clp;
  149.     register int    cbo;
  150.     register int    success;
  151.     int        pptr;
  152.     char        opat[NPAT];
  153.     VOID        ungetkey();
  154.  
  155. #ifndef NO_MACRO
  156.     if(macrodef) {
  157.         ewprintf("Can't isearch in macro");
  158.         return FALSE;
  159.     }
  160. #endif
  161.     for (cip=0; cip<NSRCH; cip++)
  162.         cmds[cip].s_code = SRCH_NOPR;
  163.     (VOID) strcpy(opat, pat);
  164.     cip = 0;
  165.     pptr = -1;
  166.     clp = curwp->w_dotp;
  167.     cbo = curwp->w_doto;
  168.     is_lpush();
  169.     is_cpush(SRCH_BEGIN);
  170.     success = TRUE;
  171.     is_prompt(dir, TRUE, success);
  172.     for (;;) {
  173.         update();
  174.         switch (c = getkey(FALSE)) {
  175.         case CCHR('['):
  176.             srch_lastdir = dir;
  177.             curwp->w_markp = clp;
  178.             curwp->w_marko = cbo;
  179.             ewprintf("Mark set");
  180.             return (TRUE);
  181.  
  182.         case CCHR('G'):
  183.             if (success != TRUE) {
  184.                 while (is_peek() == SRCH_ACCM)
  185.                     is_undo(&pptr, &dir);
  186.                 success = TRUE;
  187.                 is_prompt(dir, pptr < 0, success);
  188.                 break;
  189.             }
  190.             curwp->w_dotp = clp;
  191.             curwp->w_doto = cbo;
  192.             curwp->w_flag |= WFMOVE;
  193.             srch_lastdir = dir;
  194.             (VOID) ctrlg(FFRAND, 0);
  195.             (VOID) strcpy(pat, opat);
  196.             return ABORT;
  197.  
  198.         case CCHR(']'):
  199.         case CCHR('S'):
  200.             if (dir == SRCH_BACK) {
  201.                 dir = SRCH_FORW;
  202.                 is_lpush();
  203.                 is_cpush(SRCH_FORW);
  204.                 success = TRUE;
  205.             }
  206.             if (success==FALSE && dir==SRCH_FORW)
  207.                 break;
  208.             is_lpush();
  209.             pptr = strlen(pat);
  210.             (VOID) forwchar(FFRAND, 1);
  211.             if (is_find(SRCH_FORW) != FALSE) is_cpush(SRCH_MARK);
  212.             else {
  213.                 (VOID) backchar(FFRAND, 1);
  214.                 ttbeep();
  215.                 success = FALSE;
  216.             }
  217.             is_prompt(dir, pptr < 0, success);
  218.             break;
  219.  
  220.         case CCHR('R'):
  221.             if (dir == SRCH_FORW) {
  222.                 dir = SRCH_BACK;
  223.                 is_lpush();
  224.                 is_cpush(SRCH_BACK);
  225.                 success = TRUE;
  226.             }
  227.             if (success==FALSE && dir==SRCH_BACK)
  228.                 break;
  229.             is_lpush();
  230.             pptr = strlen(pat);
  231.             (VOID) backchar(FFRAND, 1);
  232.             if (is_find(SRCH_BACK) != FALSE) is_cpush(SRCH_MARK);
  233.             else {
  234.                 (VOID) forwchar(FFRAND, 1);
  235.                 ttbeep();
  236.                 success = FALSE;
  237.             }
  238.             is_prompt(dir, pptr < 0, success);
  239.             break;
  240.  
  241.         case CCHR('H'):
  242.         case CCHR('?'):
  243.             is_undo(&pptr, &dir);
  244.             if (is_peek() != SRCH_ACCM) success = TRUE;
  245.             is_prompt(dir, pptr < 0, success);
  246.             break;
  247.  
  248.         case CCHR('\\'):
  249.         case CCHR('Q'):
  250.             c = (char) getkey(FALSE);
  251.             goto  addchar;
  252.         case CCHR('M'):
  253.             c = CCHR('J');
  254.             goto  addchar;
  255.  
  256.         default:
  257.             if (ISCTRL(c)) {
  258.                 ungetkey(c);
  259.                 curwp->w_markp = clp;
  260.                 curwp->w_marko = cbo;
  261.                 ewprintf("Mark set");
  262.                 curwp->w_flag |= WFMOVE;
  263.                 return    TRUE;
  264.             }    /* and continue */
  265.         case CCHR('I'):
  266.         case CCHR('J'):
  267.         addchar:
  268.             if (pptr == -1)
  269.                 pptr = 0;
  270.             if (pptr == 0)
  271.                 success = TRUE;
  272.             pat[pptr++] = c;
  273.             if (pptr == NPAT) {
  274.                 ewprintf("Pattern too long");
  275.                 return FALSE;
  276.             }
  277.             pat[pptr] = '\0';
  278.             is_lpush();
  279.             if (success != FALSE) {
  280.                 if (is_find(dir) != FALSE)
  281.                     is_cpush(c);
  282.                 else {
  283.                     success = FALSE;
  284.                     ttbeep();
  285.                     is_cpush(SRCH_ACCM);
  286.                 }
  287.             } else
  288.                 is_cpush(SRCH_ACCM);
  289.             is_prompt(dir, FALSE, success);
  290.         }
  291.     }
  292.     /*NOTREACHED*/
  293. }
  294.  
  295. static VOID
  296. is_cpush(cmd) register int cmd; {
  297.     if (++cip >= NSRCH)
  298.         cip = 0;
  299.     cmds[cip].s_code = cmd;
  300. }
  301.  
  302. static VOID
  303. is_lpush() {
  304.     register int    ctp;
  305.  
  306.     ctp = cip+1;
  307.     if (ctp >= NSRCH)
  308.         ctp = 0;
  309.     cmds[ctp].s_code = SRCH_NOPR;
  310.     cmds[ctp].s_doto = curwp->w_doto;
  311.     cmds[ctp].s_dotp = curwp->w_dotp;
  312. }
  313.  
  314. static VOID
  315. is_pop() {
  316.     if (cmds[cip].s_code != SRCH_NOPR) {
  317.         curwp->w_doto  = cmds[cip].s_doto;
  318.         curwp->w_dotp  = cmds[cip].s_dotp;
  319.         curwp->w_flag |= WFMOVE;
  320.         cmds[cip].s_code = SRCH_NOPR;
  321.     }
  322.     if (--cip <= 0)
  323.         cip = NSRCH-1;
  324. }
  325.  
  326. static int
  327. is_peek() {
  328.     return cmds[cip].s_code;
  329. }
  330.  
  331. /* this used to always return TRUE (the return value was checked) */
  332. static VOID
  333. is_undo(pptr, dir) register int *pptr; register int *dir; {
  334.     register int    redo = FALSE ;
  335.     switch (cmds[cip].s_code) {
  336.     case SRCH_BEGIN:
  337.     case SRCH_NOPR:
  338.         *pptr = -1;
  339.     case SRCH_MARK:
  340.         break;
  341.  
  342.     case SRCH_FORW:
  343.         *dir = SRCH_BACK;
  344.         redo = TRUE;
  345.         break;
  346.  
  347.     case SRCH_BACK:
  348.         *dir = SRCH_FORW;
  349.         redo = TRUE;
  350.         break;
  351.  
  352.     case SRCH_ACCM:
  353.     default:
  354.         *pptr -= 1;
  355.         if (*pptr < 0)
  356.             *pptr = 0;
  357.         pat[*pptr] = '\0';
  358.         break;
  359.     }
  360.     is_pop();
  361.     if (redo) is_undo(pptr, dir);
  362. }
  363.  
  364. static int
  365. is_find(dir) register int dir; {
  366.     register int    plen, odoto;
  367.     register LINE    *odotp ;
  368.  
  369.     odoto = curwp->w_doto;
  370.     odotp = curwp->w_dotp;
  371.     plen = strlen(pat);
  372.     if (plen != 0) {
  373.         if (dir==SRCH_FORW) {
  374.             (VOID) backchar(FFARG | FFRAND, plen);
  375.             if (forwsrch() == FALSE) {
  376.                 curwp->w_doto = odoto;
  377.                 curwp->w_dotp = odotp;
  378.                 return FALSE;
  379.             }
  380.             return TRUE;
  381.         }
  382.         if (dir==SRCH_BACK) {
  383.             (VOID) forwchar(FFARG | FFRAND, plen);
  384.             if (backsrch() == FALSE) {
  385.                 curwp->w_doto = odoto;
  386.                 curwp->w_dotp = odotp;
  387.                 return FALSE;
  388.             }
  389.             return TRUE;
  390.         }
  391.         ewprintf("bad call to is_find");
  392.         return FALSE;
  393.     }
  394.     return FALSE;
  395. }
  396.  
  397. /*
  398.  * If called with "dir" not one of SRCH_FORW
  399.  * or SRCH_BACK, this routine used to print an error
  400.  * message. It also used to return TRUE or FALSE,
  401.  * depending on if it liked the "dir". However, none
  402.  * of the callers looked at the status, so I just
  403.  * made the checking vanish.
  404.  */
  405. static VOID
  406. is_prompt(dir, flag, success) {
  407.     if (dir == SRCH_FORW) {
  408.         if (success != FALSE)
  409.             is_dspl("I-search", flag);
  410.         else
  411.             is_dspl("Failing I-search", flag);
  412.     } else if (dir == SRCH_BACK) {
  413.         if (success != FALSE)
  414.             is_dspl("I-search backward", flag);
  415.         else
  416.             is_dspl("Failing I-search backward", flag);
  417.     } else ewprintf("Broken call to is_prompt");
  418. }
  419.  
  420. /*
  421.  * Prompt writing routine for the incremental search.
  422.  * The "prompt" is just a string. The "flag" determines
  423.  * whether pat should be printed.
  424.  */
  425. static VOID
  426. is_dspl(prompt, flag) char *prompt; {
  427.  
  428.     if (flag != FALSE)
  429.         ewprintf("%s: ", prompt);
  430.     else
  431.         ewprintf("%s: %s", prompt, pat);
  432. }
  433.  
  434. /*
  435.  * Query Replace.
  436.  *    Replace strings selectively.  Does a search and replace operation.
  437.  */
  438. /*ARGSUSED*/
  439. queryrepl(f, n)
  440. {
  441.     register int    s;
  442.     register int    rcnt = 0;    /* Replacements made so far    */
  443.     register int    plen;        /* length of found string    */
  444.     char        news[NPAT];    /* replacement string        */
  445.  
  446. #ifndef NO_MACRO
  447.     if(macrodef) {
  448.         ewprintf("Can't query replace in macro");
  449.         return FALSE;
  450.     }
  451. #endif
  452.     if ((s=readpattern("Query replace")) != TRUE)
  453.         return (s);
  454.     if ((s=ereply("Query replace %s with: ",news, NPAT, pat)) == ABORT)
  455.         return (s);
  456.     if (s == FALSE)
  457.         news[0] = '\0';
  458.     ewprintf("Query replacing %s with %s:", pat, news);
  459.     plen = strlen(pat);
  460.  
  461.     /*
  462.      * Search forward repeatedly, checking each time whether to insert
  463.      * or not.  The "!" case makes the check always true, so it gets put
  464.      * into a tighter loop for efficiency.
  465.      */
  466.  
  467.     while (forwsrch() == TRUE) {
  468.     retry:
  469.         update();
  470.         switch (getkey(FALSE)) {
  471.         case 'y':
  472.         case 'Y':
  473.         case ' ':
  474.             if (lreplace((RSIZE) plen, news, f) == FALSE)
  475.                 return (FALSE);
  476.             rcnt++;
  477.             break;
  478.  
  479.         case '.':
  480.             if (lreplace((RSIZE) plen, news, f) == FALSE)
  481.                 return (FALSE);
  482.             rcnt++;
  483.             goto stopsearch;
  484.  
  485.         case CCHR('G'): /* ^G or ESC */
  486.             (VOID) ctrlg(FFRAND, 0);
  487.         case CCHR('['):
  488.             goto stopsearch;
  489.  
  490.         case '!':
  491.             do {
  492.                 if (lreplace((RSIZE) plen, news, f) == FALSE)
  493.                     return (FALSE);
  494.                 rcnt++;
  495.             } while (forwsrch() == TRUE);
  496.             goto stopsearch;
  497.  
  498.                 case 'n':
  499.         case 'N':
  500.         case CCHR('H'):
  501.         case CCHR('?'):        /* To not replace */
  502.             break;
  503.  
  504.         default:
  505. ewprintf("<SP> replace, [.] rep-end, <DEL> don't, [!] repl rest <ESC> quit");
  506.             goto retry;
  507.         }
  508.     }
  509. stopsearch:
  510.     curwp->w_flag |= WFHARD;
  511.     update();
  512.     if (rcnt == 0)
  513.         ewprintf("(No replacements done)");
  514.     else if (rcnt == 1)
  515.         ewprintf("(1 replacement done)");
  516.     else
  517.         ewprintf("(%d replacements done)", rcnt);
  518.     return TRUE;
  519. }
  520.  
  521. /*
  522.  * This routine does the real work of a
  523.  * forward search. The pattern is sitting in the external
  524.  * variable "pat". If found, dot is updated, the window system
  525.  * is notified of the change, and TRUE is returned. If the
  526.  * string isn't found, FALSE is returned.
  527.  */
  528. forwsrch() {
  529.     register LINE    *clp;
  530.     register int    cbo;
  531.     register LINE    *tlp;
  532.     register int    tbo;
  533.     char        *pp;
  534.     register int    c;
  535.  
  536.     clp = curwp->w_dotp;
  537.     cbo = curwp->w_doto;
  538.     for(;;) {
  539.         if (cbo == llength(clp)) {
  540.             if((clp = lforw(clp)) == curbp->b_linep) break;
  541.             cbo = 0;
  542.             c = CCHR('J');
  543.         } else
  544.             c = lgetc(clp, cbo++);
  545.         if (eq(c, pat[0]) != FALSE) {
  546.             tlp = clp;
  547.             tbo = cbo;
  548.             pp  = &pat[1];
  549.             while (*pp != 0) {
  550.                 if (tbo == llength(tlp)) {
  551.                     tlp = lforw(tlp);
  552.                     if (tlp == curbp->b_linep)
  553.                         goto fail;
  554.                     tbo = 0;
  555.                     c = CCHR('J');
  556.                 } else
  557.                     c = lgetc(tlp, tbo++);
  558.                 if (eq(c, *pp++) == FALSE)
  559.                     goto fail;
  560.             }
  561.             curwp->w_dotp  = tlp;
  562.             curwp->w_doto  = tbo;
  563.             curwp->w_flag |= WFMOVE;
  564.             return TRUE;
  565.         }
  566.     fail:    ;
  567.     }
  568.     return FALSE;
  569. }
  570.  
  571. /*
  572.  * This routine does the real work of a
  573.  * backward search. The pattern is sitting in the external
  574.  * variable "pat". If found, dot is updated, the window system
  575.  * is notified of the change, and TRUE is returned. If the
  576.  * string isn't found, FALSE is returned.
  577.  */
  578. backsrch() {
  579.     register LINE    *clp;
  580.     register int    cbo;
  581.     register LINE    *tlp;
  582.     register int    tbo;
  583.     register int    c;
  584.     register char    *epp;
  585.     register char    *pp;
  586.  
  587.     for (epp = &pat[0]; epp[1] != 0; ++epp)
  588.         ;
  589.     clp = curwp->w_dotp;
  590.     cbo = curwp->w_doto;
  591.     for (;;) {
  592.         if (cbo == 0) {
  593.             clp = lback(clp);
  594.             if (clp == curbp->b_linep)
  595.                 return FALSE;
  596.             cbo = llength(clp)+1;
  597.         }
  598.         if (--cbo == llength(clp))
  599.             c = CCHR('J');
  600.         else
  601.             c = lgetc(clp,cbo);
  602.         if (eq(c, *epp) != FALSE) {
  603.             tlp = clp;
  604.             tbo = cbo;
  605.             pp  = epp;
  606.             while (pp != &pat[0]) {
  607.                 if (tbo == 0) {
  608.                     tlp = lback(tlp);
  609.                     if (tlp == curbp->b_linep)
  610.                         goto fail;
  611.                     tbo = llength(tlp)+1;
  612.                 }
  613.                 if (--tbo == llength(tlp))
  614.                     c = CCHR('J');
  615.                 else
  616.                     c = lgetc(tlp,tbo);
  617.                 if (eq(c, *--pp) == FALSE)
  618.                     goto fail;
  619.             }
  620.             curwp->w_dotp  = tlp;
  621.             curwp->w_doto  = tbo;
  622.             curwp->w_flag |= WFMOVE;
  623.             return TRUE;
  624.         }
  625.     fail:    ;
  626.     }
  627.     /*NOTREACHED*/
  628. }
  629.  
  630. /*
  631.  * Compare two characters.
  632.  * The "bc" comes from the buffer.
  633.  * It has its case folded out. The
  634.  * "pc" is from the pattern.
  635.  */
  636. static int
  637. eq(bc, pc)
  638. register int bc, pc;
  639. {
  640.     bc = CHARMASK(bc);
  641.     pc = CHARMASK(pc);
  642.     if (bc == pc) return TRUE;
  643.     if (ISUPPER(bc)) return TOLOWER(bc) == pc;
  644.     if (ISUPPER(pc)) return bc == TOLOWER(pc);
  645.     return FALSE;
  646. }
  647.  
  648. /*
  649.  * Read a pattern.
  650.  * Stash it in the external variable "pat". The "pat" is
  651.  * not updated if the user types in an empty line. If the user typed
  652.  * an empty line, and there is no old pattern, it is an error.
  653.  * Display the old pattern, in the style of Jeff Lomicka. There is
  654.  * some do-it-yourself control expansion.
  655.  */
  656. readpattern(prompt) char *prompt; {
  657.     register int    s;
  658.     char        tpat[NPAT];
  659.  
  660.     if (tpat[0] == '\0') s = ereply("%s: ", tpat, NPAT, prompt);
  661.     else s = ereply("%s: (default %s) ", tpat, NPAT, prompt, pat);
  662.  
  663.     if (s == TRUE)                /* Specified        */
  664.         (VOID) strcpy(pat, tpat);
  665.     else if (s==FALSE && pat[0]!=0)        /* CR, but old one    */
  666.         s = TRUE;
  667.     return s;
  668. }
  669.